<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Task extends CI_Controller {
    
    public function __construct(){
        parent::__construct();
        
        //check login
        $userId = $this->session->userdata('UserId'); 
        if(empty($userId)){
            redirect('/user/login');
        }
    }
    
    /**
     * chaneg status  
     * @author tien.nguyen
     */
    public function change_status($taskId, $status, $refHttp = ''){        
        $this->load->model('Tasks', null, true);        
        if($this->Tasks->changeStatus($taskId, $status)){
            if(!empty($_SERVER['HTTP_REFERER'])){
                redirect($_SERVER['HTTP_REFERER']);    
            }
        }
    }
    
    /**
     * show detail
     * @author tien.nguyen
     */
    public function detail($taskId){
        $this->load->model('Tasks', null, true);
        $task = $this->Tasks->getTask($taskId);
        
        $data['Task'] = $task;
        $this->load->view('task/detail', $data);
    }
    
    /**
     * Index action 
     * @author tien.nguyen
     */
	public function index($page = 1){
	    $configPagination = $this->config->item('number_pagination');
        //instance model
        $this->load->model('Tasks', '', true);
        
        $this->load->library('pagination');
        $config['base_url'] = site_url().'/task/index/';
        $config['total_rows'] = $this->Tasks->getCountTask($this->session->userdata['UserId']);
        $config['per_page'] = $configPagination['task'];
        $this->pagination->initialize($config); 
        
        $page = empty($page) ? 0 : $page;
        //load data by model 
        $data['Tasks'] = $this->Tasks->getListTask($this->session->userdata['UserId'],
             $page);
        $data['Pagination'] = $this->pagination;
		$this->load->view('task/index', $data);
	}
}